﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;

namespace Commons.ASPNETCore
{

    public record Result(CodeType Code, bool IsSuccess, string? Msg, object? Data)
    {

        public static Result Create(CodeType code, bool isSuccess, string? msg, object? data)
        {
            return new Result(code, isSuccess, msg, data);
        }
        /// <summary>
        /// 返回成功
        /// </summary>
        /// <returns></returns>
        public static Result Success()
        {
            return Create(CodeType.OK, true, "操作成功", null);
        }

        /// <summary>
        /// 返回成功
        /// </summary>
        /// <param name="response">返回的数据</param>
        /// <returns></returns>
        public static Result Success<T>(T response)
        {
            return Create(CodeType.OK, true, CodeType.OK.GetDescription(), response);
        }

        /// <summary>
        /// 返回失败
        /// </summary>
        /// <returns></returns>
        public static Result Fail()
        {
            return Create(CodeType.OK, false,"操作失败", data: default);
        }
        /// <summary>
        /// 返回失败
        /// </summary>
        /// <param name="code">错误代码</param>
        /// <returns></returns>
        public static Result Fail(CodeType code)
        {
            return Create(CodeType.OK, false, code.GetDescription(), data: default);
        }
        /// <summary>
        /// 返回失败
        /// </summary>
        /// <param name="Code">消息</param>
        /// <param name="response">返回的数据</param>
        /// <returns></returns>
        public static Result Fail(CodeType code, object response)
        {
            return Create(code,isSuccess: false, code.GetDescription(), response);
        }

        /// <summary>
        /// 返回失败结果
        /// </summary>
        /// <param name="code">错误代码</param>
        /// <param name="msg">错误消息</param>
        /// <param name="data">返回的数据</param>
        /// <returns></returns>
        public static Result Fail(CodeType code, string? msg = null, object? data = null)
        {
            return Create(code, false, msg ?? code.GetDescription(), data);
        }
    }
}
